using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CourseSync.LibFetcher.Filter
{
    public class SplitFilter : Filter
    {
        protected Regex m_Prefix;
        protected Regex m_Postfix;

        public override MatchCollection GetMatches(string content)
        {
            return null;
        }

        public string[] Split(string content)
        {
            List<string> retval = new List<string>();
            int start_offset = 0;
            while (true)
            {
                Match m1 = m_Prefix.Match(content, start_offset);
                if ((m1 == null) || (!m1.Success))
                    break;
                Match m2 = m_Postfix.Match(content, m1.Index + m1.Length);
                if ((m2 == null) || (!m2.Success))
                    break;
                start_offset = m2.Index + m2.Length;
                retval.Add(content.Substring(m1.Index + m1.Length, m2.Index - m1.Index - m1.Length));
            }
            return retval.ToArray();
        }

        public SplitFilter(Regex prefix, Regex postfix)
        {
            m_Prefix = prefix;
            m_Postfix = postfix;
        }

        public SplitFilter(string prefix, string postfix)
            : this(new Regex(prefix, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled),
                   new Regex(postfix, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled))
        {
        }
    }
}
